<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class krs_approve extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_krs_approve');
		$this->load->model('mdl_krs');
		$this->load->model('mdl_mahasiswa');
		
		//$this->output->enable_profiler(TRUE);
	}
	
	function index(){
		$data = array(
			'tahun' => $this->cek_krs(true)
		);
		$this->load->view('registrasi/krs_approve', $data);
	}
	
	# grid
	function grid(){
		$data = $this->mdl_krs_approve->getdata();
		echo $this->mdl_krs_approve->togrid($data['row_data'], $data['row_count']);
	}
	
	function grid_detail($id){
		$data = $this->mdl_krs_approve->getdata_detail($id);
		echo $this->mdl_krs_approve->togrid($data['row_data'], $data['row_count']);
	}
	# end grid
	
	function approve($id){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		$result = $this->mdl_krs_approve->approve($id, 'Approve');
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}

	function cek_krs($return = false){
		$thn = $this->mdl_krs->get_tahuns();
		if($thn->num_rows() > 0) {
			$thnV = $thn->result();
			foreach($thnV as $tahun) {
				if($this->mdl_krs->cek_krs($tahun->id_tahunajaran)) {
					if($return) {
						return $tahun->id_tahunajaran;
					}
					echo $tahun->id_tahunajaran;
					return;
				}
			}
		}
		if($return)
			return false;
		echo 'false';
	}
	
}